var AlarmCountBoard = function () {
  var template = /* html */ `
    <div class="alarm-count-board" v-loading.fullscreen.lock="loading">
      <search-btns :pagination="pagination"></search-btns>
      <template v-if="alarmData!==null">
        <div class="alarm-count-board-machine">
          <alarm-count-machine-chart :alarm-data="alarmData" :pagination="pagination"></alarm-count-machine-chart>
        </div>
        <div class="alarm-count-board-alarm">
          <alarm-count-alarm-chart :alarm-data="alarmData"></alarm-count-alarm-chart>
        </div>
      </template>
    </div>
  `;

  return {
    mixins: [boardRunningMixin],

    data: function () {
      return {
        loading: false,
        alarmData: null,
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        }
      };
    },

    created: function () {
      this.loading = true;
      var self = this;
      this.setAlarmData(true, true).then(function () {
        self.boardRunning();
        self.loading = false;
      });
    },

    methods: {
      setAlarmData: function (hasAlert, isInit) {
        if (hasAlert === undefined) {
          hasAlert = false;
        }

        var self = this;
        return ajaxRequest('workOrder/getWorkAlarmSummary', {}, true, hasAlert).then(function (res) {
          if (!isInit) {
            return res;
          }

          var alarmData = new AlarmCountInfo(res);
          self.alarmData = alarmData;
          self.pagination.totalCount = alarmData.machineAlarmData.length;
          return alarmData;
        }).catch(function () {
          return null;
        });
      },

      boardRunning: function () {
        this.clearTimeoutList();
        this.resetAlarmDataRunning();
      },

      resetAlarmDataRunning: function () {
        var tomorrowMoment = moment().add(1, 'day').startOf('day');
        var delayTime = tomorrowMoment.diff(moment());
        delayTime = Math.min(delayTime, 30 * 60 * 1000);

        var self = this;
        var timeout = setTimeout(function () {
          self.setAlarmData().then(function (res) {
            var alarmData = self.alarmData;
            alarmData.resetAlarmData(res);
            var totalCount = alarmData.machineAlarmData.length;
            var pagination = self.pagination;
            var pageSize = pagination.pageSize;
            var totalPage = Math.ceil(totalCount / pageSize);
            if (pagination.currentPage > totalPage) {
              pagination.currentPage = 1;
            }
            self.resetAlarmDataRunning();
          });
          self.timeoutList[0] = null;
        }, delayTime);
        this.timeoutList[0] = timeout;
      }
    },

    components: {
      SearchBtns: SearchBtns,
      AlarmCountMachineChart: AlarmCountMachineChart,
      AlarmCountAlarmChart: AlarmCountAlarmChart
    },

    template: template
  };
}();
